package com.atguigu.chapter06;

import com.atguigu.bean.OrderEvent;
import com.atguigu.bean.TxEvent;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/17 16:23
 */
public class Flink04_Project_Order {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
    
        SingleOutputStreamOperator<OrderEvent> orderEventStream = env
            .readTextFile("input/OrderLog.csv")
            .map(line -> {
                String[] data = line.split(",");
                return new OrderEvent(Long.valueOf(data[0]), data[1], data[2], Long.valueOf(data[3]));
            })
            .filter(log -> "pay".equals(log.getEventType()));
    
        SingleOutputStreamOperator<TxEvent> txEventStream = env
            .readTextFile("input/ReceiptLog.csv")
            .map(line -> {
                String[] data = line.split(",");
                return new TxEvent(data[0], data[1], Long.valueOf(data[2]));
            });
    
        orderEventStream
            .connect(txEventStream)
            .keyBy(OrderEvent::getTxId, TxEvent::getTxId)
            .process(new CoProcessFunction<OrderEvent, TxEvent, String>() {
                // key都是交易id
                Map<String, OrderEvent> orderEventMap = new HashMap<>();
                Map<String, TxEvent> txEventMap = new HashMap<>();
                @Override
                public void processElement1(OrderEvent orderEvent,
                                            Context ctx,
                                            Collector<String> out) throws Exception {
                    // 订单信息来了, 先去txEventMap去查看对应的交易信息有没有
                    // 如果有, 对账成功
                    // 如果没有, 把自己写入到 orderEventMap中
    
                    TxEvent txEvent = txEventMap.get(orderEvent.getTxId());
                    if (txEvent != null) {
                        out.collect(orderEvent.getOrderId() + " 对账成功");
                    }else{
                        orderEventMap.put(orderEvent.getTxId(), orderEvent);
                    }
                }
            
                @Override
                public void processElement2(TxEvent txEvent,
                                            Context ctx,
                                            Collector<String> out) throws Exception {
                    OrderEvent orderEvent = orderEventMap.get(txEvent.getTxId());
                    if (orderEvent != null) {
                        out.collect(orderEvent.getOrderId() + " 对账成功");
                    }else{
                        txEventMap.put(txEvent.getTxId(), txEvent);
                    }
                }
            })
            .print();
        
    
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

}
